package com.ct.lotterysystem2.controller;

import com.ct.lotterysystem2.common.errorcode.ControllerErrorCodeConstants;
import com.ct.lotterysystem2.common.exception.ControllerException;
import com.ct.lotterysystem2.common.pojo.CommonResult;
import com.ct.lotterysystem2.common.utils.JacksonUtil;
import com.ct.lotterysystem2.controller.param.CreateActivityParam;
import com.ct.lotterysystem2.controller.param.PageParam;
import com.ct.lotterysystem2.controller.result.CreateActivityResult;
import com.ct.lotterysystem2.controller.result.FindActivityListResult;
import com.ct.lotterysystem2.service.ActivityService;
import com.ct.lotterysystem2.service.dto.ActivityDTO;
import com.ct.lotterysystem2.service.dto.CreateActivityDTO;
import com.ct.lotterysystem2.service.dto.PageListDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

@Slf4j
@RestController
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(
            @Validated @RequestBody CreateActivityParam param) {
        log.info("createActivity CreateActivityParam:{}",
                JacksonUtil.writerValueAsString(param));
        CreateActivityDTO createActivityDTO = activityService.createActivity(param);
        return CommonResult.success(convertToCreateActivityResult(createActivityDTO));
    }


    public CommonResult<FindActivityListResult> findActivityList(PageParam param) {
        log.info("findActivityList PageParam:{}",
                JacksonUtil.writerValueAsString(param));
        PageListDTO<ActivityDTO> pageListDTO = activityService.findActivityList(param);
        return CommonResult.success(convertToFindActivityListResult(pageListDTO));
    }

    private FindActivityListResult convertToFindActivityListResult(PageListDTO<ActivityDTO> pageListDTO) {
        FindActivityListResult result = new FindActivityListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(pageListDTO.getRecords()
                .stream()
                .map(activityDTO -> {
                    FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
                    activityInfo.setActivityId(activityDTO.getActivityId());
                    activityInfo.setActivityName(activityDTO.getActivityName());
                    activityInfo.setDescription(activityDTO.getDescription());
                    activityInfo.setValid(activityDTO.Valid());
                    return activityInfo;
                }).collect(Collectors.toList())
        );
        return result;
    }

    private CreateActivityResult convertToCreateActivityResult(CreateActivityDTO createActivityDTO) {
        if (null == createActivityDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }
        CreateActivityResult result = new CreateActivityResult();
        result.setActivityId(createActivityDTO.getActivityId());
        return result;
    }
}
